package com.kongge.viewdisplayframeworklib.model;

import com.kongge.viewdisplayframeworklib.control.GlobalMagager;
import com.kongge.viewdisplayframeworklib.control.PublicInterface;
import com.kongge.viewdisplayframeworklib.event.action.EQAction;
import com.kongge.viewdisplayframeworklib.event.action.EQGotoFrameAction;

import java.util.ArrayList;
import java.util.List;

public class PageControlStack {

	private ArrayList<AbsUIController> mControls;
	private int mStackId;
	private int mTabIndex;

	
	public AbsUIController getCurPageControl() {
		if (mControls != null && mControls.size() > 0) {
			return mControls.get(mControls.size() - 1);
		}
		return null;
	}
	
	public AbsUIController getCurFirstPageControl() {
		if (mControls != null && mControls.size() > 0) {
			return mControls.get(0);
		}
		return null;
	}
	
	public List<AbsUIController> getPageControlList() {
		return mControls;
	}
	
	public String getCurPageTitle() {
		return mControls.get(mControls.size() - 1).getTitle();
	}

	public boolean canGoBack() {
		return mControls.size() > 1;
	}

	public PageControlStack() {
		mControls = new ArrayList<AbsUIController>();
		mTabIndex = -1;
	}

	public int getTabIndex() {
		return mTabIndex;
	}

	public void setTabIndex(int nTabIndex) {
		mTabIndex = nTabIndex;
	}

	public void setStackId(int nStackId) {
		mStackId = nStackId;
	}

	public AbsUIController getPageControl(PageControlStack lastPageStack, EQAction gotoFrameAction) {
		int nPageId = ((EQGotoFrameAction) gotoFrameAction).getGotoFrameId();
		AbsUIController curUIController = lastPageStack.getCurPageControl();
		AbsUIController control = null;
		for (int nIndex = 0; nIndex < mControls.size(); ++ nIndex) {
			AbsUIController tempControl = mControls.get(nIndex);
			if (tempControl.getPageId() == nPageId) {
				curUIController = tempControl;
				break;
			}
		}
		
		if (control == null) {
			control = PublicInterface.createUIControl(gotoFrameAction);
		}
		adjustStack(curUIController, control);
		return control;
	}
	
	public AbsUIController getPageControl(int nPageId) {
		AbsUIController curUIController = getCurPageControl();
        AbsUIController control = null;
		for (int nIndex = 0; nIndex < mControls.size(); ++nIndex) {
			AbsUIController tempControl = mControls.get(nIndex);
			if (tempControl.getPageId() == nPageId) {
				curUIController = tempControl;
				break;
			}
		}

		if (control == null) {
			control = PublicInterface.createUIControl(nPageId);
		}
		adjustStack(curUIController, control);
		return control;
	}

	private void adjustStack(AbsUIController lastUIController, AbsUIController topControl) {
		// TODO Auto-generated method stub
		if (topControl != null) {
			int nStackLevel = topControl.getStackLevel();
			if (mControls.size() > 0) {

				for (int i = mControls.size() - 1; i >= 0; i--) {
					AbsUIController tempControl = mControls.get(i);
					if (tempControl.getStackLevel() > nStackLevel) {
						mControls.get(i).onBackground();
						mControls.get(i).onRemove();
						mControls.remove(i);
					} else if (tempControl.getStackLevel() == nStackLevel) {
						if (tempControl == topControl) {
							if (lastUIController != null) {
								lastUIController.onBackground();
							}
							break;
						} else {
							mControls.get(i).onBackground();
							mControls.get(i).onRemove();
							mControls.remove(i);
							mControls.add(topControl);
							break;
						}
					} else {
						if (lastUIController != null) {
							lastUIController.onBackground();
						}
						/*if (mControls.size() > 0) {
							mControls.get(mControls.size() - 1).onBackground();
						}*/
						mControls.add(topControl);
						break;
					}

				}

			} else {
				if (mControls.size() > 0) {
					mControls.get(mControls.size() - 1).onBackground();
				}
				mControls.add(topControl);
			}
		}
	}

	public boolean isPageInStackList(int nPageId) {
		PageStackList stackList = GlobalMagager.getPageNodeManager()
				.getPageStackListByStackId(mStackId);
		if (stackList != null) {
			return stackList.isInList(nPageId);
		}
		return false;
	}

	public AbsUIController getTopUIControl() {
		if (mControls != null && !(mControls.isEmpty())) {
			return mControls.get(mControls.size() - 1);
		}
		return null;
	}

	public AbsUIController goBack() {
		if (mControls != null && mControls.size() > 1) {
			mControls.get(mControls.size() - 1).onBackground();
			mControls.get(mControls.size() - 1).onRemove();
			mControls.remove(mControls.size() - 1);
			return mControls.get(mControls.size() - 1);
		}
		return null;
	}

	public void init(int nDefaultId) {
		if (mControls.size() <= 0) {
			AbsUIController curPage = getCurPageControl();
            AbsUIController control = PublicInterface.createUIControl(nDefaultId);
			adjustStack(curPage, control);
		}
	}
	
	public void removeAllStack() {
		if (mControls != null) {
			mControls = null;
		}
	}
}
